package com.easyui.shiro;

import com.easyui.pojo.Menu;
import com.easyui.service.MenuService;
import org.apache.shiro.config.Ini;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {

    @Autowired
    private MenuService menuService;
    private static String filterChainDefinitions;

    public void setFilterChainDefinitions(String definitions) {
        filterChainDefinitions = definitions;
        Ini ini = new Ini();
        ini.load(definitions);
        Ini.Section section = ini.getSection("urls");
        if (CollectionUtils.isEmpty(section)) {
            section = ini.getSection("");
        }
        List<Menu> menuList = menuService.getAllMenu();
        for (Menu menu : menuList) {
            if (!StringUtils.isEmpty(menu.getUrl())) {
                //  user.html?act=page  roles[admin,test]
                //user.html  perms[5]
                String url = menu.getUrl();
                if (url.indexOf("?") >= 0) {
                    url = url.substring(0, url.lastIndexOf("?"));
                }
                section.put(url, "perms[" + menu.getId() + "]");
            }
        }
        section.put("/**", "authc");
        this.setFilterChainDefinitionMap(section);
    }

    public void update() {
        synchronized (this) {   //强制同步，控制线程安全
            try {
                AbstractShiroFilter shiroFilter = (AbstractShiroFilter) this.getObject();
                PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter
                        .getFilterChainResolver();
                // 过滤管理器
                DefaultFilterChainManager manager = (DefaultFilterChainManager) resolver.getFilterChainManager();
                // 清除权限配置
                manager.getFilterChains().clear();
                this.getFilterChainDefinitionMap().clear();
                // 重新设置权限
                this.setFilterChainDefinitions(filterChainDefinitions);

                Map<String, String> chains = this.getFilterChainDefinitionMap();
                if (!CollectionUtils.isEmpty(chains)) {
                    Iterator var12 = chains.entrySet().iterator();
                    while (var12.hasNext()) {
                        Map.Entry<String, String> entry = (Map.Entry) var12.next();
                        String url = (String) entry.getKey();
                        String chainDefinition = (String) entry.getValue();
                        manager.createChain(url, chainDefinition);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
